package com.project.petLossSystem.controller.system;

import com.project.petLossSystem.common.CustomException;
import com.project.petLossSystem.service.PetCategoryService;
import com.project.petLossSystem.util.Result;
import com.project.petLossSystem.util.ResultGenerator;
import com.project.petLossSystem.vo.petCategory.FirstPetCategoryVO;
import com.project.petLossSystem.vo.petCategory.SecondPetCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class SystemPetCategoryController {
    @Autowired
    private PetCategoryService petCategoryService;
    @GetMapping("/firstPetCategories")
    @ResponseBody
    public Result firstPetCategories(){
        List<FirstPetCategoryVO> firstPetCategories = petCategoryService.getCategoriesForIndex();
        if(CollectionUtils.isEmpty(firstPetCategories)){
            CustomException.failure("分类数据异常！"); // 一级分类都没有已经是问题了
        }
        return ResultGenerator.successResult(firstPetCategories);
    }

    @GetMapping("/secondPetCategories")
    @ResponseBody
    public Result secondPetCategories(@RequestParam("categoryId") Long categoryId){
        List<SecondPetCategoryVO> secondPetCategories = petCategoryService.getSecondCategories(categoryId);
        if(CollectionUtils.isEmpty(secondPetCategories)){
            return ResultGenerator.failureResult("无二级分类！"); // 二级分类不存在无需报错
        }
        return ResultGenerator.successResult(secondPetCategories);
    }
}
